package com.idealighter.game.games.texaspoker.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.dic.TexaporkerRoomDic;
import com.idealighter.game.dictionary.domain.TexaporkerRoomDomain;
import com.idealighter.game.games.texaspoker.struct.TexapokerRoom;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * .. 德州扑克数据管理 .
 * 
 * @date 2015年8月6日 下午9:39:01
 */
@Singleton
public class TexapokerDataMgr {

  private static final Logger LOG = LoggerFactory.getLogger(TexapokerDataMgr.class);

  private TexaporkerRoomDic roomDic;

  // 德州扑克游戏房间
  private final Map<Integer, TexapokerRoom> rooms;
  // 玩家座位
  private final Map<Long, TexapokerRoom.Table.Seat> playerSeats = new HashMap<>();

  @Inject
  public TexapokerDataMgr(TexaporkerRoomDic roomDic) {
    this.roomDic = roomDic;
    this.rooms = createRooms();
  }

  /**
   * 创建房间 .
   * 
   * @return 房间map.
   */
  private Map<Integer, TexapokerRoom> createRooms() {
    Map<Integer, TexapokerRoom> rooms = new HashMap<>();
    for (TexaporkerRoomDomain roomDomain : roomDic.list()) {
      rooms.put(roomDomain.getId(),
          new TexapokerRoom(roomDomain.getId(), roomDomain.getName(), roomDomain.getTable()));
      LOG.info("[德州扑克]创建房间[{}][{}]", roomDomain.getId(), roomDomain.getName());
    }

    return rooms;
  }

  /**
   * 获取房间 .
   * 
   * @return 房间map.
   */
  public Map<Integer, TexapokerRoom> getRooms() {

    return rooms;
  }

  /**
   * 获取房间.
   * 
   * @param roomId . 房间Id.
   * @return 房间.
   */
  public TexapokerRoom getRoom(int roomId) {

    return rooms.get(roomId);
  }

  /**
   * 获取玩家的座位.
   * 
   * @return 房间.
   */
  public TexapokerRoom.Table.Seat getPlayerSeat(long playerId) {

    return playerSeats.get(playerId);
  }

  /**
   * 获取房间id.
   * 
   * @param tableId . 桌子Id.
   * @return 房间id.
   */
  public int getRoomId(int tableId) {

    return tableId / TexapokerRoom.Table.MAX_TABLE;
  }

  /**
   * 获取牌桌 .
   * 
   * @param tableId . 桌子Id.
   * @return 桌子.
   */
  public TexapokerRoom.Table getTable(int tableId) {
    TexapokerRoom room = getRoom(getRoomId(tableId));

    if (room == null) {
      return null;
    } else {
      return room.tables.get(tableId);
    }

  }

  /**
   * 更新玩家座位.
   * 
   * @param playerId 玩家id. 玩家id.
   * @param seat 座位信息. 座位.
   */
  public void updatePlayerSeats(long playerId, TexapokerRoom.Table.Seat seat) {
    playerSeats.put(playerId, seat);
  }

  /**
   * 移除玩家座位.
   * 
   * @param playerId 玩家id. 玩家Id.
   */
  public void removePlayerSeat(long playerId) {
    playerSeats.remove(playerId);
  }
}
